Skip to main content

Working with Entities in ApertureDB

Entities are a way to represent any application level concept in ApertureDB like a Person, Product, Event, etc

# Connector class offers a way to connect to an instance of ApertureDB
from aperturedb import Connector

db = Connector.Connector("aperturedb.local", user="admin", password="admin")

# Simple query to see how the database is doing
# https://docs.aperturedata.io/query_language/Reference/db_commands/GetStatus
query = [{
"GetStatus": {
}
}]

# Execute the query to get back a JSON response for GetStatus
response, blobs = db.query(query)

db.print_last_response()
[
{
"GetStatus": {
"info": "OK",
"status": 0,
"system": "ApertureDB",
"version": "0.15.11"
}
}
]

One way to introduce new entities in the system is through our query language

For bulk additions, we recommend using the Python SDK loaders

query = [{
"AddEntity": {
"class": "Person", # Every entity belongs to a class, in this case Person
"properties": { # You can define as many key, value properties per entity, as required
"name": "Mary", # The data type for this property for a Person is now set to string.
"lastname": "Teresa"
},
"if_not_found": { # conditional add
"lastname": ["==", "Teresa"]
}
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"AddEntity": {
"status": 0
}
}
]

Verify this entity was added to the database and read all its properties

query = [{
"FindEntity": {
"with_class": "Person", # Every entity belongs to a class, in this case Person
"constraints": {
"name": ["==", "Mary"],
"lastname": ["==", "Teresa"]
},
"results": {
"all_properties": True
}
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"FindEntity": {
"entities": [
{
"_uniqueid": "13.0.47894344407220",
"lastname": "Teresa",
"name": "Mary"
}
],
"returned": 1,
"status": 0
}
}
]

Use UpdateEntity if any of the attributes need a new value or your application now needs a new attribute in existing entities

query = [{
"UpdateEntity": {
"with_class": "Person",
"properties": {
"known_for": "Nobel Peace Prize",
"name": "Mother"
},
"constraints": {
"name": ["==", "Mary"],
"lastname": ["==", "Teresa"]
},
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"UpdateEntity": {
"count": 1,
"status": 0
}
}
]
query = [{
"FindEntity": {
"with_class": "Person",
"constraints": {
"lastname": ["==", "Teresa"]
},
"results": {
"all_properties": True
}
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"FindEntity": {
"entities": [
{
"_uniqueid": "13.0.47894344407220",
"known_for": "Nobel Peace Prize",
"lastname": "Teresa",
"name": "Mother"
}
],
"returned": 1,
"status": 0
}
}
]

We can now delete the entity

query = [{
"DeleteEntity": {
"with_class": "Person",
"constraints": {
"lastname": ["==", "Teresa"]
}
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"DeleteEntity": {
"count": 1,
"status": 0
}
}
]

We can verify that the entity is not longer in the database with another FindEntity

query = [{
"FindEntity": {
"with_class": "Person",
"constraints": {
"lastname": ["==", "Teresa"]
},
"results": {
"all_properties": True
}
}
}]

response, blobs = db.query(query)

db.print_last_response()
[
{
"FindEntity": {
"returned": 0,
"status": 0
}
}
]